package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.EleSafeManagement;
import com.example.entity.PointDefend;
import com.example.entity.PointFlow;
import com.example.mapp.EleSafeManagementMapp;
import com.example.mapp.PointFlowMapp;
import com.example.service.EleSafeManageService;
import com.example.util.*;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by wang on 2018/4/30.
 */
@Service
public class EleSafeManageServiceImpl implements EleSafeManageService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    EleSafeManagementMapp eleSafeManagementMapp;

    @Autowired
    PointFlowMapp pointFlowMapp;

    @Override
    public Map<String, Object> eleSafeManageUpload(EleSafeManagement eleSafeManagement) {
        try {
            EleSafeManagement orgId = eleSafeManagementMapp.queryByOrgId(eleSafeManagement.getOrgId());
            if (null == orgId) {
                eleSafeManagementMapp.addEleSafeManagement(eleSafeManagement);
            } else {
                eleSafeManagementMapp.update(eleSafeManagement);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(),null);
        }finally {
            if(StringUtil.isNotBlank(eleSafeManagement.getLoggerType())){
                if("2".equals(eleSafeManagement.getLoggerType()) && "16".equals(eleSafeManagement.getRoleId()) ){
                    // 每一次操作都会进行判断 ,通过唯一标识获取配置信息
                    PointDefendInfoCache infoCache = new PointDefendInfoCache();
                    PointDefend pointDefend = infoCache.getKeyByValue("elevator_safe_manager_build");
                    if(pointDefend !=null){
                        // 获取必填字段
                        List<String> strings = Arrays.asList(pointDefend.getMustBean().split(","));

                        double i = ObjectBeanUtils.percentage(eleSafeManagement, strings);
                        if (i != 0) {
                            // 查询角色信息
                            Map<String, Object> maps = pointFlowMapp.findUserInfo(Long.valueOf(eleSafeManagement.getLoggerPartyId()));
                            // 计算分数
                            int round = (int) (i * pointDefend.getMark());
                            // 判断原来的分数是否为 0
                            long mark = (long) maps.get("mark");
                            if (mark != 0) {
                                // 通过唯一标识查询流水表获取上一次积分，然后减去
                                PointFlow pointFlow = new PointFlow();
                                pointFlow.setIdentification("elevator_safe_manager_build");
                                List<PointFlow> list = pointFlowMapp.findOne(pointFlow);
                                if (list != null && list.size() > 0) {
                                    round = (int) ((mark - list.get(0).getMark()) + round);
                                }else{
                                    round = (int) (mark + round);
                                }
                            }
                            // 当进行此处操作时，添加的是单位的分数
                            String userId = (String) maps.get("userId");
                            int i1 = pointFlowMapp.updateMark(round, userId);
                            //
                            String userName = "电梯安全管理员";
                            String partyName = (String) maps.get("partyName");
                            SystemPointUtil.insertFlow(pointFlowMapp,i1,pointDefend,round,userName,partyName,"电梯安全管理制度的建立及完善加单位分数");

                            // 添加安全管理管理员的分数
                            PointDefend pointUserDefend = infoCache.getKeyByValue("elevator_safe_manager_build_user");

                            Map<String, Object> usermaps = pointFlowMapp.findUserInfoByUserId(Long.valueOf(eleSafeManagement.getLoggerUserId()));

                            // 计算分数
                            int roundUsers = (int) (i * pointUserDefend.getMark());

                            // 判断原来的分数是否为 0
                            long markUsers = (long) usermaps.get("mark");
                            if(markUsers != 0){
                                // 通过唯一标识查询流水表获取上一次积分，然后减去
                                PointFlow pointFlow = new PointFlow();
                                pointFlow.setIdentification("elevator_safe_manager_build_user");
                                List<PointFlow> list = pointFlowMapp.findOne(pointFlow);
                                if (list != null && list.size() > 0) {
                                    roundUsers = (int) ((markUsers - list.get(0).getMark()) + roundUsers);
                                }else{
                                    roundUsers = (int) (markUsers + roundUsers);
                                }
                            }
                            int i2 = pointFlowMapp.updateMark(roundUsers, eleSafeManagement.getLoggerUserId());
                            String userName2 = "电梯安全管理员";
                            String partyName2 = (String) maps.get("partyName");
                            SystemPointUtil.insertFlow(pointFlowMapp,i2,pointUserDefend,roundUsers,userName2,partyName2,"电梯安全管理制度的建立及完善加管理员分数");

                        }
                    }
                }
            }
        }
        return ResultUtil.returnSuccess("上传文件成功",null);
    }

    @Override
    public Map<String, Object> eleSafeManageQuery(String orgId) {
        Map<String,Object> map = new HashMap<String,Object>();
        Object ret = null;
        try {
            ret = eleSafeManagementMapp.queryByOrgId(orgId);
            map.put("list",ret);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }
}
